package tiger.android_hierarchy_view;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class SelectBrokerListAdapter extends BaseAdapter implements
		OnClickListener {

	private Context mContext;
	private List<BrokerOrgEntity> mList;
	private List<BrokerOrgEntity> mCurrentList = new ArrayList<BrokerOrgEntity>();
	private OnClickNameListener mNameListener;

	public interface OnClickNameListener {
		public void onClickName(BrokerOrgEntity model);
	}

	public SelectBrokerListAdapter(Context context, List<BrokerOrgEntity> list) {
		mContext = context;
		mList = list;
		filterDate(mList);
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		mCurrentList.clear();
		filterDate(mList);
		System.out.println("#size:" + mCurrentList.size());
	}

	@Override
	public void onClick(View v) {
		ViewHolder holderView = (ViewHolder) v.getTag();
		// if (v.getId() == R.id.tv_orgname) {
		// mNameListener.onClickName(holderView.getModel());
		// } else if (v.getId() == R.id.iv_arrow) {
		expandOrCollapse(holderView);
		// }
	}

	private void expandOrCollapse(ViewHolder holder) {
		holder.getModel().setExpand(!holder.getModel().isExpand());
		notifyDataSetChanged();
	}

	private List<BrokerOrgEntity> filterDate(List<BrokerOrgEntity> list) {
		if (list == null)
			list = new ArrayList<BrokerOrgEntity>();
		for (BrokerOrgEntity item : list) {
			mCurrentList.add(item);
			if (item.isExpand())
				filterDate(item.getList());
		}
		return list;
	}

	@Override
	public int getCount() {
		return mCurrentList.size();
	}

	@Override
	public Object getItem(int position) {
		return mCurrentList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(mContext,
					R.layout.select_broker_list_item, null);
		}
		holder = new ViewHolder(
				(TextView) convertView.findViewById(R.id.brokerName), null,
				mCurrentList.get(position));
		holder.getName().setText(holder.getModel().getOrgName());
		holder.getName().setPadding(20 + 15 * holder.getModel().getLevel(),
				holder.getName().getPaddingTop(),
				holder.getName().getPaddingRight(),
				holder.getName().getPaddingBottom());
		// holder.getArrow().setVisibility(
		// holder.getModel().hasChild() ? View.VISIBLE : View.GONE);
		// holder.getArrow().setImageResource(
		// holder.getModel().isExpand() ? R.drawable.expand_fold_down
		// : R.drawable.expand_fold_right);
		// holder.getArrow().setOnClickListener(this);
		holder.getName().setOnClickListener(this);
		// holder.getArrow().setTag(holder);
		holder.getName().setTag(holder);
		return convertView;
	}

	private class ViewHolder {
		private ImageView arrow;
		private TextView name;
		private BrokerOrgEntity model;

		public ViewHolder(TextView tv, ImageView img, BrokerOrgEntity mo) {
			name = tv;
			arrow = img;
			model = mo;
		}

		public ImageView getArrow() {
			return arrow;
		}

		public TextView getName() {
			return name;
		}

		public BrokerOrgEntity getModel() {
			return model;
		}

	}

}
